package cn.fl.warrant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.warrant.service.IArcBorrowFaceService;
import cn.fl.warrant.transform.ArcBorrowFaceVOTransform;
import cn.fl.warrant.vo.ArcBorrowFaceVO;
import fl.warrant.facade.archive.api.IArcBorrowFaceApiService;

/**
 * @Description: 面对面借阅 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ArcBorrowFaceServiceImpl implements IArcBorrowFaceService {
	
	private static final Logger logger = LoggerFactory.getLogger(IArcBorrowFaceService.class);
	
	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApi;
	
	@Autowired
	private IArcBorrowFaceApiService arcBorrowFaceApiService;
	
	
	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
	
	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvDescList(String grpCd) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCodeNm());
				keyValue.setValue(sysDictionaryDTO.getCodeDesc());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
	
	@Override
	public ArcBorrowFaceVO selectById(Long id) throws BaseException{
		return ArcBorrowFaceVOTransform.toVO(arcBorrowFaceApiService.selectById(id));
	}

	@Override
	public Long add(ArcBorrowFaceVO entity) throws BaseException{
		return this.arcBorrowFaceApiService.add(ArcBorrowFaceVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		return this.arcBorrowFaceApiService.removeById(id);
	}
	


	@Override
	public boolean update(ArcBorrowFaceVO entity) throws BaseException{
		return this.arcBorrowFaceApiService.update(ArcBorrowFaceVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		return this.arcBorrowFaceApiService.searchListPage(page);
	}
	
}
